<?php

namespace App\Http\Controllers\Home\shopCar;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class shopCarController extends Controller
{
    //购物车列表
    public function index(Request $request)
    {

        $id = $request -> goods_id;

        if($id == '')
        {
            $data = DB::table('cart')
            ->leftJoin('goods', 'cart.goods_id', '=', 'goods.id')
            ->select('cart.*', 'goods.logo', 'goods.goods_name', 'goods.goods_price')
            ->get();
        }
        else
        {
            $cart = array();
            $cart['goods_id'] = $id;
            $cart['goods_number'] = 1;
            $cart['member_id'] = session('master')['id'];
            $res = DB::table('cart') -> insert($cart);
            if($res)
            {
                 $data = DB::table('cart')
                    ->leftJoin('goods', 'cart.goods_id', '=', 'goods.id')
                    ->select('cart.*', 'goods.logo', 'goods.goods_name', 'goods.goods_price')
                    ->get();
            }
        }
       
    	return view('home.shopCar.shopCar', ['data' => $data]);
    }

    public function ajaxUpdate(Request $request)
    {
    	$id = $request -> id;

    	$num = $request -> num;
    	//修改数量
    	$res = DB::table('cart') -> where('id', $id) -> update(['goods_number' => $num]);

    	if($res)
    	{
    		return 1;
    	}else
    	{
    		return 0;
    	}
    }

    public function delete(Request $request)
    {
    	$id = $request -> id;

    	$data = DB::table('cart') -> delete($id);

    	if($data)
    	{
    		return redirect('/home/shopCar/shopCar');
    	}else
    	{
    		return back();
    	}
    }

}
